"""
套接字非阻塞示例
"""
from socket import *
from time import sleep, ctime

f = open('log.txt', 'a+')
# tcp套接字
sockfd = socket()
sockfd.bind(('0.0.0.0', 8888))
sockfd.listen(3)
# 设置套接字为非阻塞状态
# sockfd.setblocking(False)

# 超时检测
sockfd.settimeout(3)

while True:
    print('Waiting for connect...')
    try:
        connfd, addr = sockfd.accept()
    # except Exception as e:
    except (BlockingIOError, timeout) as e:
        # 每隔2s写入一条日志
        sleep(2)
        f.write('%s:%s\n' % (ctime(), e))
        f.flush()
    else:
        data = connfd.recv(1024).decode()
        print(data)
